package com.jintian.smart.kernel.autoconfigure.extend;

import lombok.Data;
import org.springframework.core.env.Environment;

@Data
public class SnowProperties {
    private int timeBits;
    private int workerBits;
    private int seqBits;
    private String epochStr;
    private int boostPower;
    private int paddingFactor;
    private int scheduleInterval;
    private static final SnowProperties defaultConfig = new SnowProperties(28, 22, 13, "2025-05-01", 3, 50, 60);

    public SnowProperties() {
        initByDefault();
    }

    public SnowProperties(final int timeBits, final int workerBits, final int seqBits, final String epochStr, final int boostPower, final int paddingFactor, final int scheduleInterval) {
        this.timeBits = timeBits;
        this.workerBits = workerBits;
        this.seqBits = seqBits;
        this.epochStr = epochStr;
        this.boostPower = boostPower;
        this.paddingFactor = paddingFactor;
        this.scheduleInterval = scheduleInterval;
    }

    public SnowProperties(Environment environment) {
        if (environment == null) {
            initByDefault();
        } else {
            initByEnvironment(environment);
        }
    }

    private void initByEnvironment(Environment env) {
        String prefix = "smart.snow";
        timeBits = env.getProperty(prefix + ".timeBits", Integer.class, defaultConfig.getTimeBits());
        workerBits = env.getProperty(prefix + ".workerBits", Integer.class, defaultConfig.getWorkerBits());
        seqBits = env.getProperty(prefix + ".seqBits", Integer.class, defaultConfig.getSeqBits());
        epochStr = env.getProperty(prefix + ".epochStr", defaultConfig.getEpochStr());
        boostPower = env.getProperty(prefix + ".boostPower", Integer.class, defaultConfig.getBoostPower());
        paddingFactor = env.getProperty(prefix + ".paddingFactor", Integer.class, defaultConfig.getPaddingFactor());
        scheduleInterval = env.getProperty(prefix + ".scheduleInterval", Integer.class, defaultConfig.getScheduleInterval());
    }

    private void initByDefault() {
        this.timeBits = defaultConfig.timeBits;
        this.workerBits = defaultConfig.workerBits;
        this.seqBits = defaultConfig.seqBits;
        this.epochStr = defaultConfig.epochStr;
        this.boostPower = defaultConfig.boostPower;
        this.paddingFactor = defaultConfig.paddingFactor;
        this.scheduleInterval = defaultConfig.scheduleInterval;
    }
}
